import { Injectable } from '@angular/core';
import { AppConfig } from '../config';
import { Headers, Http } from '@angular/http';
import { ModelParam } from '../entity/model-Param';
import { Observable } from "rxjs/Observable";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class ModelParamService {
    private param: string = "modelParams";
    private get url(): string {
        return `${AppConfig.APP_RESTFUL}/${this.param}`;
    }

    constructor(private http: InterceptorService) { }

    getModelParams(): Promise<ModelParam[]> {
        return this.http.get(`${this.url}`)
            .toPromise()
            .then(response => response.json().data as ModelParam[])
            .catch(this.handleError);

    }
    /**
   * 根据型号Id获取型号参数
   * 
   * @param {number} modelId 
   * @returns {Promise<Tag[]>} 
   * @memberof ModelService
   */
    getModelParamsByModeID(modeID: number): Promise<ModelParam[]> {
        return this.http.get(`${this.url}/modelId/${modeID}`)
            .toPromise()
            .then(response => response.json().data as ModelParam[])
            .catch(this.handleError);
    }
    getModelParamsByDeviceCategoryId(deviceCategoryId: number): Promise<ModelParam[]> {
        return this.http.get(`${this.url}/deviceCategoryId/${deviceCategoryId}`)
            .toPromise()
            .then(response => response.json().data as ModelParam[])
            .catch(this.handleError);
    }
    create(modelParam: ModelParam): Promise<ModelParam> {

        //let url="http://192.168.15.150:8585/webapi/v1/modelParams";
        return this.http.post(this.url, JSON.stringify(modelParam))
            .toPromise()
            .then(res => {
                try {
                    let result = res.json();
                    switch (result.code) {
                        case "200":
                            return result.data as ModelParam;
                        case "-1":
                            throw new Error(result.message);
                    }
                }
                catch (error) {
                    this.handleError(error);
                }
            })
            .catch(this.handleError);
    }
    update(modelParam: ModelParam): Promise<ModelParam> {
        return this.http.put(this.url, JSON.stringify(modelParam))
            .toPromise()
            .then(() => ModelParam)
            .catch(this.handleError);
    }
    delete(id: number): Promise<void> {
        const url = `${this.url}/${id}`;
        return this.http.delete(url)
            .toPromise()
            .then(() => null)
            .catch(this.handleError);
    }
    batchDelete(valueIds: number[]): Promise<boolean> {
        const apiUrl = `${this.url}?items=${valueIds}`;
        return this.http.delete(apiUrl)
            .toPromise()
            .then(response => {
                try {
                    let result = response.json();
                    switch (result.code) {
                        case "200":
                            return true;
                        case "-1":
                            throw new Error(result.message);
                    }
                }
                catch (error) {
                    throw error;
                }

            })
            .catch(this.handleError);
    }
    hasmodelParamName(modelParamName: string, deviceCategoryId: number): Observable<boolean> {
        const url = `${this.url}/deviceCategoryId/${deviceCategoryId}/modelParamName/${modelParamName}`;
        return this.http.get(url).map(response => {
            let code = response.json().code;
            switch (code) {
                case "3001":
                    return true;
                case "3002":
                    return false;
            };
        }).catch(this.handleError);
    }
    hasmodelParamKey(modelParamKey: string, deviceCategoryId: number): Observable<boolean> {
        const url = `${this.url}/deviceCategoryId/${deviceCategoryId}/modelParamKey/${modelParamKey}`;
        return this.http.get(url).map(response => {
            let code = response.json().code;
            switch (code) {
                case "3001":
                    return true;
                case "3002":
                    return false;
            };
        }).catch(this.handleError);
    }
    private handleError(error: any): Promise<any> {
        // console.error('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }

}


// WEBPACK FOOTER //
// ./src/app/service/model-param.service.ts